<?php

class DefaultController extends FrontController
{
	public function actionIndex()
	{
		$this->pageTitle = $this->getSiteNameByLanguage().' | '.Yii::t('site/general','Products');
        $this->render('index');
	}
    
    public function actionView()
    {
        $product_id = $this->get('id',0);
        $product_alias = $this->get('alias','');
        $locale = $this->get('locale','');
        $cat_id = $this->get('cat_id',0);
        $model = LanguageProduct::model()->with('product')->find(
            'product_id=:productId AND alias=:alias AND locale=:locale AND product.category_id=:catId AND product.status=:status',
            array(
                ':productId' => $product_id,
                ':alias' => $product_alias,
                ':locale' => $locale,
                ':catId' => $cat_id,
                ':status' => Product::STATUS_ACTIVE,
            )
        );
        if (is_null($model))
            throw new CHttpException(404,Yii::t('site/product','Product is not found'));
        $this->pageTitle = $this->getSiteNameByLanguage().' | '.$model->name;
        $this->render('view',array('model'=>$model));
    }
    
    public function actionListByCategory()
    {
        $cat_id = $this->get('id',0);
        $alias = $this->get('alias','');
        $locale = $this->get('locale','');
        $cat = LanguageCategory::model()->with('category')->find(
            't.category_id=:catId AND t.alias=:alias AND locale=:locale AND category.is_active = 1',
            array(
                ':catId' => $cat_id,
                ':alias' => $alias,
                ':locale' => $locale,
            )
        );
        if (is_null($cat))
            throw new CHttpException(404,Yii::t('site/general','CATEGORY_NOT_FOUND'));
        
        $this->pageTitle = $this->getSiteNameByLanguage().' | '.$cat->title;
        $this->render('list_by_category',array('cat'=>$cat));    
    }
}